package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import pojo.Customer;
import test.MD5Tool;
import util.DataConn;

public class CustomerDao {
	public Customer login(Customer c) {
		Customer cu=null;
		Connection conn=DataConn.getConn();
		String sql="select * from customer where c_name=? and c_pass=?";
		try {
			PreparedStatement ps=conn.prepareStatement(sql);
			ps.setString(1, c.getC_name());
			ps.setString(2, MD5Tool.stringToMD5(c.getC_pass()));
			ResultSet rs=ps.executeQuery();
			if(rs.next()) {
				cu=new Customer();
				cu.setC_name(rs.getString("c_name"));
				cu.setC_pass(rs.getString("c_pass"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return cu;
	}
	public int regCustomer(String name,String pwd) {
		int flag=0;
		Connection conn=DataConn.getConn();
		String sql="insert into customer(c_name,c_pass) values(?,?)";
		try {
			PreparedStatement ps=conn.prepareStatement(sql);
			ps.setString(1, name);
			ps.setString(2,pwd);
			flag=ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	public int update(Customer user) {
		int flag =0;
		//连接数据库
		Connection conn =DataConn.getConn();
		//编写SQL语句
		String sql="update customer set c_pass=? where c_name=?";
		try {
			PreparedStatement ps=conn.prepareStatement(sql);
			ps.setString(1,user.getC_pass());
			ps.setString(2,user.getC_pass());
			//ps.setString(17,user.getC_birthday());
			flag=ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return flag;
	}
}
